package com.yidu.DailyFinalTreatment.controller;

import com.yidu.BusinessParameters.entity.CashAccruedEntity;
import com.yidu.BusinessParameters.entity.SecurityAccruedEntity;
import com.yidu.BusinessParameters.mapper.CashAccruedMapper;
import com.yidu.BusinessParameters.service.SecurityAccruedService;
import com.yidu.CashAdministration.entity.MoneyAllocateEntity;
import com.yidu.CashAdministration.service.MoneyAllocateService;
import com.yidu.DailyFinalTreatment.entity.BondInterestIncomeEntity;
import com.yidu.DailyFinalTreatment.entity.CashInventMo;
import com.yidu.DailyFinalTreatment.entity.IncomePaymentEntity;
import com.yidu.DailyFinalTreatment.entity.PayTwoFeesEntity;
import com.yidu.DailyFinalTreatment.mapper.BondSecurityMapper;
import com.yidu.DailyFinalTreatment.service.IncomePaymentService;
import com.yidu.StockControl.entity.CashInventory;
import com.yidu.StockControl.entity.SecurityInventory;
import com.yidu.StockControl.mapper.CashInventoryMapper;
import com.yidu.StockControl.mapper.CashInventorysMapper;
import com.yidu.StockControl.service.SecurityInventoryService;
import com.yidu.Util.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IncomePaymentController {
    @Autowired
    IncomePaymentService incomePaymentService;
    @Autowired
    Dbutil dbutil;
    @Autowired
    private SecurityAccruedService securityAccruedService;
    @Autowired
    private MoneyAllocateService moneyAllocateService;
    @Autowired
    private CashAccruedMapper cashInventoryMapper;
    @Autowired
    CashInventorysMapper cashInventorysMapper;
    @Autowired
    CashInventoryMapper cashInventoryMappers;

    /**
     * 现金利息收入 页面
     * @param page
     * @param limit
     * @param date
     * @return
     */
    @RequestMapping("cashInterestIncome")
    public Map cashInterestIncome(int page, int limit,String date){
        List<IncomePaymentEntity> moneyAllocateEntities = incomePaymentService.selectCashInterestIncome(page,limit,date);
        Map map = new HashMap();
        map.put("code",0);
        map.put("count",0);
        map.put("msg","");
        map.put("data",moneyAllocateEntities);
        System.out.println(moneyAllocateEntities+"afadf");
        System.out.println("日期是"+date);
        return map;
    }

    /**
     * 债卷利息 页面
     * @param page
     * @param limit
     * @param date
     * @return
     */
    @RequestMapping("BondInterestIncome")
    public Map bondInterestIncome(int page, int limit,String date){
        List<BondInterestIncomeEntity> bondInterestIncomeEntities = incomePaymentService.selectBondInterestIncome(page,limit,date);
        Map map = new HashMap();
        map.put("code",0);
        map.put("count",0);
        map.put("msg","");
        map.put("data",bondInterestIncomeEntities);
        return map;
    }

    /**
     * 支付两费 页面
     * @param page
     * @param limit
     * @param date
     * @return
     */
    @RequestMapping("payTwoFees")
    public Map payTwoFees(int page, int limit,String date){
        List<PayTwoFeesEntity> payTwoFeesEntities = incomePaymentService.selectPayTwoFees(page,limit,date);
        Map map = new HashMap();
        map.put("code",0);
        map.put("count",0);
        map.put("msg","");
        map.put("data",payTwoFeesEntities);
        return map;

    }

    /**
     * 现金应收应付收入
     * @param cash
     * @return
     */
    @RequestMapping("insertIncomePaymentIncome")
    public int incomePaymentIncome(String cash, HttpServletRequest request){
        HttpSession session = request.getSession();
        String  fundId = (String) session.getAttribute("fundId");
        System.out.println("sdad"+cash);
        List<IncomePaymentEntity> incomePaymentEntities = SysUtil.jsonToArrayList(cash, IncomePaymentEntity.class);
        CashInventory cashInventory = new CashInventory();
        // 现金
        CashAccruedEntity cashAccrued = new CashAccruedEntity();
        int i=0;
        // 资金调拨
        MoneyAllocateEntity moneyAllocate = new MoneyAllocateEntity();
        for (IncomePaymentEntity IncomePayment:incomePaymentEntities) {

            cashAccrued.setBusinessType(IncomePayment.getBusinessType());
            cashAccrued.setMoneyFlow(IncomePayment.getBusinessStatu());
            cashAccrued.setMoneySun(IncomePayment.getCashTotalAmount());
            cashAccrued.setCashDate(IncomePayment.getCashInventoryTime());
            System.out.println(incomePaymentEntities+"aerrrr"+cashAccrued);

            List<CashAccruedEntity> cashAccruedEntities = incomePaymentService.cashAccruedID(cashAccrued);
            System.out.println("----------------------"+cashAccruedEntities);
            for (CashAccruedEntity CashAccrued:cashAccruedEntities){
                //删除现金应收应付
                cashInventoryMappers.deleteCashInventory(CashAccrued.getCashAccruedId());
                System.out.println(CashAccrued.getCashAccruedId()+"======");
            }
            //添加现金应收应付
            String maxId = dbutil.getMaxId3(TableIdUtil.CAID, TableNameUtil.CA, TableTimeUtil.CATime,IncomePayment.getCashInventoryTime());

            cashAccrued.setCashAccruedId(maxId);
            cashAccrued.setFundId(fundId);
            cashAccrued.setCashId("C20220628001");
            cashAccrued.setCashDate(IncomePayment.getCashInventoryTime());
            cashAccrued.setMoneySun(IncomePayment.getCashTotalAmount());
            cashAccrued.setMoneyFlow(IncomePayment.getBusinessType());
            cashAccrued.setBusinessType(IncomePayment.getBusinessType());
            System.out.println("现金应收应付库存"+cashAccrued);
            i = cashInventoryMapper.cashInventoryinsert(cashAccrued);
            //资金调拨
            String maxing = dbutil.getMaxId3(TableIdUtil.FAID, TableNameUtil.MA, TableTimeUtil.FATime,IncomePayment.getCashInventoryTime());
            moneyAllocate.setMoneyAllocateId(maxing);
            moneyAllocate.setAllocateFlow(IncomePayment.getBusinessStatu()*-1);
            moneyAllocate.setAllocateMoney(IncomePayment.getCashTotalAmount());
            moneyAllocate.setCashId("C20220628001");
            moneyAllocate.setAllocateDate(IncomePayment.getCashInventoryTime());
            moneyAllocate.setAllocateType(IncomePayment.getBusinessType());
            moneyAllocate.setBusinessId("b10");
            moneyAllocate.setFundId(fundId);
            moneyAllocate.setMoneyAllocateBeiZhu("现金");
            moneyAllocate.setBusinessDate(IncomePayment.getCashInventoryTime());
            moneyAllocateService.moneyAllocateInsert(moneyAllocate);
        }

        return i;
    }
    /**
     * 债卷利息收入
     * @param Securities
     * @param accountIds
     */
    @RequestMapping("insertDataBondInterestIncome")
    public int BondInterestIncome(String Securities,String accountIds) {
        System.out.println(Securities + "++accountIds");
        List<BondInterestIncomeEntity> bondInterestIncomeEntities = SysUtil.jsonToArrayList(Securities, BondInterestIncomeEntity.class);
        //BondInterestIncomeEntity bondInterestIncomeEntity = new BondInterestIncomeEntity();
        int j = 0;
        //证券
        SecurityAccruedEntity securityAccrued = new SecurityAccruedEntity();
        //资金调拨
        MoneyAllocateEntity moneyAllocate = new MoneyAllocateEntity();

        //SecurityInventory securityInventory = new SecurityInventory();
        for (BondInterestIncomeEntity bondInterestIncome : bondInterestIncomeEntities) {
            System.out.println(bondInterestIncome + "数据");
            //bondInterestIncomeEntity 更改数据
            securityAccrued.setMoneyFlow(bondInterestIncome.getBusinessStatu());
            securityAccrued.setSecurityDate(bondInterestIncome.getSecurityInventoryTime());
            securityAccrued.setSecurityName(bondInterestIncome.getSecurityName());
            securityAccrued.setMoney(bondInterestIncome.getSecurityTotalAmount());
            securityAccrued.setBusinessType(bondInterestIncome.getSecurityType());
            //查询ID
            List<String> bondInterestIncomes = incomePaymentService.selectBondInterestIncomeID(securityAccrued);
            System.out.println(bondInterestIncomes + "我是id");
            for (String BondInterestIncomes : bondInterestIncomes) {
                //通过id删除
                  int i = incomePaymentService.deleteBondInterestIncomeID(BondInterestIncomes);
               System.out.println(BondInterestIncomes + "id数量");
            }
            String maxIds = dbutil.getMaxId3(TableIdUtil.SAID, TableNameUtil.SA, TableTimeUtil.SATime,bondInterestIncome.getSecurityInventoryTime());

            securityAccrued.setSecurityId("019604");
            securityAccrued.setSecurityDate(bondInterestIncome.getSecurityInventoryTime());
            securityAccrued.setSecurityAccruedId(maxIds);
            securityAccrued.setCashId("C20220628001");
            securityAccrued.setFundId("000899");
            securityAccrued.setBusinessType(bondInterestIncome.getSecurityType());
            securityAccrued.setMoney(bondInterestIncome.getSecurityTotalAmount());
            securityAccrued.setMoneyFlow(bondInterestIncome.getBusinessStatu());
            System.out.println(maxIds + "securityAccrued的id" +securityAccrued);
            j = securityAccruedService.securityAccruedinsert(securityAccrued);


            //资金调拨 moneyAllocate.getBusinessDate()
            String maxing = dbutil.getMaxId3(TableIdUtil.FAID, TableNameUtil.MA, TableTimeUtil.FATime,bondInterestIncome.getSecurityInventoryTime());
            moneyAllocate.setMoneyAllocateId(maxing);
            moneyAllocate.setAllocateFlow(bondInterestIncome.getBusinessStatu()*-1);
            moneyAllocate.setAllocateMoney(bondInterestIncome.getSecurityTotalAmount());
            moneyAllocate.setCashId("C20220628001");
            moneyAllocate.setAllocateDate("2022-9-11");
            moneyAllocate.setAllocateType(bondInterestIncome.getSecurityType());
            moneyAllocate.setBusinessId("b1");
            moneyAllocate.setFundId("000899");
            moneyAllocate.setMoneyAllocateBeiZhu("我是备注");
            moneyAllocate.setBusinessDate(bondInterestIncome.getSecurityInventoryTime());
            moneyAllocateService.moneyAllocateInsert(moneyAllocate);
            System.out.println(j + "增加数量");

        }
        return j;
    }

    /**
     * 两费
     * @param TwoFees
     * @return
     */
    @RequestMapping("payTwoFee")
    public int DataPayTwoFees(String TwoFees) {
        System.out.println(TwoFees);
        List<PayTwoFeesEntity> bondInterestIncomeEntities = SysUtil.jsonToArrayList(TwoFees, PayTwoFeesEntity.class);
        PayTwoFeesEntity payTwoFeesEntity = new PayTwoFeesEntity();
        CashInventory cashInventory = new CashInventory();
        int i=0;
        //现金
        CashAccruedEntity cashAccrued = new CashAccruedEntity();
        // 资金调拨
        MoneyAllocateEntity moneyAllocate = new MoneyAllocateEntity();
        for (PayTwoFeesEntity payTwoFees:bondInterestIncomeEntities) {

            cashAccrued.setBusinessType(payTwoFees.getBusinessType());
            cashAccrued.setMoneyFlow(payTwoFees.getBusinessStatu());
            cashAccrued.setMoneySun(payTwoFees.getCashTotalAmount());
            cashAccrued.setCashDate(payTwoFees.getCashInventoryTime());
            System.out.println(payTwoFees+"aerrrr"+cashAccrued);

            List<CashAccruedEntity> cashAccruedEntities = incomePaymentService.cashAccruedID(cashAccrued);
            for (CashAccruedEntity CashAccrued:cashAccruedEntities){
                //删除现金应收应付
                cashInventoryMappers.deleteCashInventory(CashAccrued.getCashAccruedId());
                System.out.println(CashAccrued.getCashAccruedId()+"adfafd");
            }
            cashAccrued.setCashAccruedId("2");
            cashAccrued.setFundId("000899");
            cashAccrued.setCashId("C20220628001");
            cashAccrued.setCashDate(payTwoFees.getCashInventoryTime());
            cashAccrued.setMoneySun(payTwoFees.getCashTotalAmount());
            cashAccrued.setMoneyFlow(payTwoFees.getBusinessType());
            cashAccrued.setBusinessType(payTwoFees.getBusinessType());
            System.out.println("现金应收应付库存"+cashAccrued);
            i = cashInventoryMapper.cashInventoryinsert(cashAccrued);
            //资金调拨
            String maxing = dbutil.getMaxId(TableIdUtil.FAID, TableNameUtil.MA, TableTimeUtil.FATime);
            moneyAllocate.setMoneyAllocateId(maxing);
            moneyAllocate.setAllocateFlow(payTwoFees.getBusinessStatu()*-1);
            moneyAllocate.setAllocateMoney(payTwoFees.getCashTotalAmount());
            moneyAllocate.setCashId("C20220628001");
            moneyAllocate.setAllocateDate("2022-9-11");
            moneyAllocate.setAllocateType(payTwoFees.getBusinessType());
            moneyAllocate.setBusinessId("b12");
            moneyAllocate.setFundId("000899");
            moneyAllocate.setMoneyAllocateBeiZhu("两费");
            moneyAllocate.setBusinessDate(payTwoFees.getCashInventoryTime());
            moneyAllocateService.moneyAllocateInsert(moneyAllocate);
        }
        return i;
    }
}
